package cn.lyq.card.ui.wallet

import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.utils.setClick
import kotlinx.android.synthetic.main.act_cash_record.*
import kotlinx.android.synthetic.main.layout_title.tvTitleSplit


class CashRecordActivity : BaseActivity() {

    private val mPageAdapter by lazy {
        FamilyPagerAdapter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_cash_record
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_cash_record)
        tvTitleSplit.visibility = View.GONE
        viewPager.adapter = mPageAdapter
        changeTab(true)
    }

    override fun initListener() {
        super.initListener()
        tvCashIng.setClick {
            changeTab(true)
        }
        tvCashEd.setClick {
            changeTab(false)
        }
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                changeTab(position == 0)
            }
        })
    }

    private fun changeTab(first: Boolean) {
        tvCashIng.isSelected = first
        tvCashEd.isSelected = !first
        viewPager.setCurrentItem(if (first) 0 else 1, true)
    }


    private class FamilyPagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int = 2

        override fun createFragment(position: Int): CashRecordFg {
            return CashRecordFg.newInstance(position)
        }

    }
}